'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');

class HouseService extends Service {
    async getList() {
        const {
            ctx,
            app
        } = this;
        // const {list} = Mock.mock({
        //     "list|5":[
        //         {
        //             "building|1":["龙华尚城","水晶花园","龚华新村","城市青年公寓"],//楼盘名称
        //             address:"北京市昌平区沙河镇顺沙路19号",//详细地址
        //             "rent|2000-5000":2000,//租金
        //             sort:1,//排序
        //             update_time:Date.now(),//更新时间
        //             state:1,//状态
        //             broker:"@cname",//经纪人
        //             "house_type|1":["新房",'二手房','出租房'],//房型
        //             "house_property|1":["商铺",'写字楼','小区'],//房产
        //             title:"@ctitle(10,20)",//标题
        //             intro:"@ctitle(10,30)",//描述
        //             "price|50-200":50,//售价
        //             "acreage|50-100":50,//面积
        //             floor_plan:"1室1厅1卫",//户型
        //             "test|1":[0,1],//验真
        //             "orientation|1":["东",'南','西','北'],//朝向
        //             area:"北京市昌平区",//区域
        //             "fitment|1":['精装修','简装修'],//装修
        //             "floor_place|1-30":1,//所在楼层
        //             floor_all:30,//总楼层
        //             name:"@cname",//联系人
        //             valid_time:Date.now()+86400000*15,
        //             cover:"@image(100x100,@color)",
        //         }
        //     ]
        // });
        // list.forEach((item)=>{
        //     app.mysql.insert('housing_resource',{
        //         ...item
        //     })
        // })
        // return '123'
        const {
            mode,
            pageSize,
            pageIndex,
            area,
            building
        } = ctx.query;
        let sql = `select * from housing_resource where 1=1 `;
        let sqlCount = `select count(id) as total from housing_resource where 1=1 `;
        // 模式
        if (mode) {
            sql += `and mode='${mode}' `
            sqlCount += `and mode='${mode}' `
        }
        // 区域
        if(area) {
            sql += `and area like '%${area}%' `
            sqlCount += `and area like '%${area}%' `
        }
        if(building) {
            sql += `and building like '%${building}%' `
            sqlCount += `and building like '%${building}%' `
        }
        //分页
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        let res = await app.mysql.query(sql);
        let [{total}] = await app.mysql.query(sqlCount);
        res.forEach((item, index) => {
            if (item.mode === '销售楼盘') {
                // 单价
                item.unitPrice = (item.price / item.acreage).toFixed(2);
            }
            // 序号
            item.number = (pageIndex-1)*5+index+1;
        })
        return {
            data:res,
            total:total
        }
    }
    // 修改状态
    async houseChange() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            state,
            broker
        } = ctx.request.body;
        let row = {
            id
        };
        if(state!==undefined) {
            row.state = state
        }
        if(broker) {
            row.broker = broker
        }
        let res = await app.mysql.update('housing_resource', row);
        return res
    }
    // 删除房源
    async delHouse() {
        const {ctx,app} = this;
        const {id} = ctx.request.body;
        let res = await app.mysql.delete('housing_resource',{id});
        return res
    }
    // 添加房源
    async addHouse() {
        const {ctx,app} = this;
        const {
            building,
            address,
            rent,
            sort,
            update_time,
            state,
            broker,
            house_type,
            house_property,
            title,
            intro,
            price,
            acreage,
            floor_plan,
            test,
            orientation,
            area,
            fitment,
            floor_place,
            floor_all,
            name,
            valid_time,
            cover,
            mode,
        } = ctx.request.body;
        let res = await app.mysql.insert('housing_resource',{
            building,
            address,
            rent,
            sort,
            update_time,
            state,
            broker,
            house_type,
            house_property,
            title,
            intro,
            price,
            acreage,
            floor_plan,
            test,
            orientation,
            area,
            fitment,
            floor_place,
            floor_all,
            name,
            valid_time,
            cover,
            mode,
        });
        return res
    }
    // 编辑房源
    async editHouse () {
        const {ctx,app} = this;
        const {
            id,
            building,
            address,
            rent,
            sort,
            update_time,
            state,
            broker,
            house_type,
            house_property,
            title,
            intro,
            price,
            acreage,
            floor_plan,
            test,
            orientation,
            area,
            fitment,
            floor_place,
            floor_all,
            name,
            valid_time,
            cover,
            mode,
        } = ctx.request.body;
        let res = await app.mysql.update('housing_resource',{
            id,
            building,
            address,
            rent,
            sort,
            update_time,
            state,
            broker,
            house_type,
            house_property,
            title,
            intro,
            price,
            acreage,
            floor_plan,
            test,
            orientation,
            area,
            fitment,
            floor_place,
            floor_all,
            name,
            valid_time,
            cover,
            mode,
        });
        return res
    }
}

module.exports = HouseService;